// \u4e00-\u9fa5] 中文验证规则
import http from 'wechat-http'
import validate from 'wechat-validate'
Page({
  behaviors: [validate],
  data: {
    id: '',
    idcardFrontUrl: '',
    idcardBackUrl: '',
    point: '',
    building: '',
    room: '',
    name: '',
    mobile: '',
    gender: '',
    title: '',
    type: '',
  },

  onLoad(options) {
    this.setData({
      type: options.type || 'add',
      id: options.id,
      point: options.point,
      building: options.building,
      room: options.room,
      name: options.name,
      gender: options.gender - '0',
      mobile: options.mobile,
      idcardFrontUrl: '/static/images/avatar_1.jpg' || options.idcardFrontUrl,
      idcardBackUrl: '/static/images/avatar_2.jpg' || options.idcardBackUrl,
    })
    wx.setNavigationBarTitle({
      title: options.type === 'modify' ? '修改业主信息' : '添加房屋信息',
    })
  },
  rules: {
    mobile: [
      { required: true, message: '请填写手机号码!' },
      {
        pattern: /^((\d3)|(\d{3}\-))?13[0-9]\d{8}|15[89]\d{8}/,
        message: '请检查手机号码是否正确!',
      },
    ],
    name: [
      { required: true, message: '请填写短信验证码!' },
      { pattern: /^[\u4e00-\u9fa5]{0,}$/, message: '请检查姓名是否正确!' },
    ],
    idcardFrontUrl: [{ required: true, message: '请完善照片信息！' }],
  },
  //发送post请求
  async goList() {
    const valid = this.validate()
    if (!valid || !this.data.idcardBackUrl || !this.data.idcardBackUrl) return
    else {
      const data = {
        point: this.data.point,
        building: this.data.building,
        room: this.data.room,
        name: this.data.name,
        gender: this.data.gender - '1',
        mobile: this.data.mobile,
        idcardFrontUrl: this.data.idcardFrontUrl,
        idcardBackUrl: this.data.idcardBackUrl,
      }
      const id = this.data.id
      const res =
        this.data.type === 'add'
          ? (console.log('添加'), await wx.http.post('/room', data))
          : await wx.http.post('/room', { id, ...data })
      console.log(res)
      wx.navigateBack({
        delta: 4, // 回退前 delta(默认为1) 页面
        success: function (res) {
          // success
        },
        fail: function () {
          // fail
        },
        complete: function () {
          // complete
        },
      })
    }
  },
  async choosePic(e) {
    const that = this
    //调用选择照片API
    await wx.chooseMedia({
      count: 1,
      mediaType: ['image'],
      sourceType: ['album'],
      success(result) {
        wx.uploadFile({
          url: 'https://live-api.itheima.net/upload',
          filePath: result.tempFiles[0].tempFilePath,
          name: 'file',
          header: {
            Authorization: `Bearer ${wx.getStorageSync('token')}`,
          },
          success(res) {
            e.currentTarget.dataset.index === '1'
              ? that.setData({ idcardFrontUrl: JSON.parse(res.data).data.url })
              : that.setData({ idcardBackUrl: JSON.parse(res.data).data.url })
          },
        })
      },
      fail() {
        console.log('获取失败')
      },
    })
  },
  removePicture(ev) {
    // 移除图片的类型（身份证正面或反面）
    const type = ev.mark?.type
    this.setData({ [type]: '' })
  },
})
